<template>
  <div class="l-from-body" style="padding: 8px 48px 8px 48px">
    <el-form
      :disabled="loading"
      :model="formData"
      :rules="rules"
      label-position="left"
      size="mini"
      ref="form"
      label-width="100px"
    >
      <el-divider content-position="left">{{ $t("密码修改") }}</el-divider>
      <el-form-item :label="$t('旧密码')" prop="oldPassword">
        <el-input v-model="formData.oldPassword" show-password></el-input>
      </el-form-item>
      <el-form-item :label="$t('新密码')" prop="password">
        <el-input v-model="formData.password" show-password></el-input>
      </el-form-item>
      <el-form-item :label="$t('重复新密码')" prop="password2">
        <el-input v-model="formData.password2" show-password></el-input>
      </el-form-item>
      <el-form-item>
        <el-button :loading="loading" type="primary" @click="onSubmit"
          >{{$t('提交')}}</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
const api = window.$api.organization.user;
export default {
  name: "user-password",
  data() {
    return {
      loading: false,
      formData: {
        oldPassword: "",
        password: "",
        password2: "",
      },
      rules: {
        oldPassword: [
          { required: true, message: this.$t("请输入旧密码") },
          { validator: this.validatorPassword, trigger: "blur" },
        ],
        password: [
          { required: true, message: this.$t("请输入新密码") },
          { validator: this.getRule, trigger: "blur" },
        ],
        password2: [
          { required: true, message: this.$t("请再输入新密码") },
          {
            validator: (rule, value, callback) => {
              if (this.formData.password2 != this.formData.password) {
                callback(new Error(this.$t("两次密码输入不一致")));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
      },
    };
  },
  computed: {},
  created() {},
  methods: {
    validatorPassword(rule, value, callback) {
      api.validatorPassword(this.$md5(value)).then((res) => {
        if (res.data.code == 200 && res.data.data) {
          callback();
        } else {
          callback(new Error(res.data.info));
        }
      });
    },
    onSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading = true;
          const req = {
            password: this.$md5(this.formData.password),
            oldPassword: this.$md5(this.formData.oldPassword),
          };
          api
            .updatePassword(req)
            .then(() => {
              this.$message({
                type: "success",
                message: this.$t("修改成功!"),
              });

              setTimeout(() => {
                this.$store.dispatch("organization/user/fedLogOut").then(() => {
                  this.$router.push({ path: "/login" });
                });
              }, 500);
            })
            .catch(() => {
              this.loading = false;
            });
        }
      });
    },
    async getRule(rule, value, callback) {
      // 密级密码验证
      // const secretData = await this.$awaitWraper(api.GetRule());
      // if (!secretData) {callback(); return}
      //  let passComplexity = secretData.data.data.f_Complexity;
      // let passLength = secretData.data.data.f_Length;
      // if (passLength > value.length) {
      //   callback(new Error("密码不符合规范"));
      // } else {
      //   // 判断对应密级密码规范
      //   let check;
      //   if (passComplexity.indexOf(1) !== -1) {
      //     // 大写
      //     check = /^(?=.*[A-Z]).*$/;
      //     if (!check.test(value)) {
      //       callback(new Error("密码不符合规范"));
      //     }
      //   } else {
      //     check = /^(?=.*[A-Z]).*$/;
      //     if (check.test(value)) {
      //       callback(new Error("密码不符合规范"));
      //     }
      //   }
      //   if (passComplexity.indexOf(2) !== -1) {
      //     // 小写
      //     check = /^(?=.*[a-z]).*$/;
      //     if (!check.test(value)) {
      //       callback(new Error("密码不符合规范"));
      //     }
      //   } else {
      //     check = /^(?=.*[a-z]).*$/;
      //     if (check.test(value)) {
      //       callback(new Error("密码不符合规范"));
      //     }
      //   }
      //   if (passComplexity.indexOf(3) !== -1) {
      //     // 数字
      //     check = /^(?=.*\d).*$/;
      //     if (!check.test(value)) {
      //       callback(new Error("密码不符合规范"));
      //     }
      //   } else {
      //     // 没有规范数字，判断是否有数字
      //     check = /^(?=.*\d).*$/;
      //     if (check.test(value)) {
      //       callback(new Error("密码不符合规范"));
      //     }
      //   }
      //   if (passComplexity.indexOf(4) !== -1) {
      //     // 特殊字符
      //     check =
      //       /^(?=.*[*?!&￥$%^#,./@";:><\\[\]}{\-=+_\\|》《。，、？’‘“”~ `]).*$/;
      //     if (!check.test(value)) {
      //       callback(new Error("密码不符合规范"));
      //     }
      //   } else {
      //     check =
      //       /^(?=.*[*?!&￥$%^#,./@";:><\\[\]}{\-=+_\\|》《。，、？’‘“”~ `]).*$/;
      //     if (check.test(value)) {
      //       callback(new Error("密码不符合规范"));
      //     }
      //   }
      //   callback()
      // }

      // 用户密码规则
      const roleData = await this.$awaitWraper(api.GetRuleByRole());
      if (!roleData) {
        callback();
        return;
      }
      let passComplexity = roleData.f_Complexity;
      let passLength = roleData.f_Length;
      if (passLength > value.length) {
        callback(new Error(this.$t("密码不符合规范")));
      } else {
        // 判断对应密级密码规范
        let check;
        if (passComplexity.indexOf(1) !== -1) {
          // 大写
          check = /^(?=.*[A-Z]).*$/;
          if (!check.test(value)) {
            callback(new Error(this.$t("密码不符合规范")));
          }
        } else {
          check = /^(?=.*[A-Z]).*$/;
          if (check.test(value)) {
            callback(new Error(this.$t("密码不符合规范")));
          }
        }
        if (passComplexity.indexOf(2) !== -1) {
          // 小写
          check = /^(?=.*[a-z]).*$/;
          if (!check.test(value)) {
            callback(new Error(this.$t("密码不符合规范")));
          }
        } else {
          check = /^(?=.*[a-z]).*$/;
          if (check.test(value)) {
            callback(new Error(this.$t("密码不符合规范")));
          }
        }
        if (passComplexity.indexOf(3) !== -1) {
          // 数字
          check = /^(?=.*\d).*$/;
          if (!check.test(value)) {
            callback(new Error(this.$t("密码不符合规范")));
          }
        } else {
          // 没有规范数字，判断是否有数字
          check = /^(?=.*\d).*$/;
          if (check.test(value)) {
            callback(new Error(this.$t("密码不符合规范")));
          }
        }
        if (passComplexity.indexOf(4) !== -1) {
          // 特殊字符
          check =
            /^(?=.*[*?!&￥$%^#,./@";:><\\[\]}{\-=+_\\|》《。，、？’‘“”~ `]).*$/;
          if (!check.test(value)) {
            callback(new Error(this.$t("密码不符合规范")));
          }
        } else {
          check =
            /^(?=.*[*?!&￥$%^#,./@";:><\\[\]}{\-=+_\\|》《。，、？’‘“”~ `]).*$/;
          if (check.test(value)) {
            callback(new Error(this.$t("密码不符合规范")));
          }
        }
        callback();
      }
    },
  },
};
</script>